//
//  LVHomeViewController.swift
//  PhotoBrowser
//
//  Created by 刘伟 on 16/4/27.
//  Copyright © 2016年 刘伟. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"

class LVHomeViewController: UICollectionViewController {

// MARK: - 懒加载属性
    private lazy var shops : [LVShop] = [LVShop]()
    private lazy var animator = LVPhotoBrowserAnimator()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
//        注册cell
        collectionView?.registerClass(LVHomeViewCell.self , forCellWithReuseIdentifier: reuseIdentifier)
        
//        发送网络请求
        loadData(0)
        
    }
}

// MARK: - 发送网络请求
extension LVHomeViewController{

    private func loadData(offset : Int){

//        这里返回的result是一个字典数组
        NetworkTools.shareInstance.loadHomeData(offset) { (result, error) -> () in
            
            //            1.错误校验
            if error != nil{
                print(error)
                return
            }
            
            //            2.获取结果
            guard let resultArray = result else{
                
                print("获取的结果不正常")
                return
            }
            
//            print(resultArray)
            //            3.遍历字典
            for resultDict in resultArray{
                
                let shop = LVShop(dict: resultDict)
                self.shops.append(shop)
            }
            
            //            4.刷新表格
            //            在闭包中使用本类中的属性,方法等需要加上self
            self.collectionView?.reloadData()
        }
        
    }

}


// MARK: - LVHomeViewController的数据源和代理方法
extension LVHomeViewController{

    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return shops.count
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
//        1.创建cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! LVHomeViewCell
        
//        2.给cell设置数据
        cell.shop = shops[indexPath.item]
        
//        3.判断是否是最后一个item出现在屏幕
        if indexPath.row == shops.count - 1{
//            如果是就加载后面的数据
            loadData(shops.count)
        }
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        showPhotoBrowser(indexPath)
    }
}


extension LVHomeViewController{
    func showPhotoBrowser(indexPath: NSIndexPath){
    
        //        点击cell时model出图片浏览控制器
        let photoBrowserVC = LVPhotoBrowserViewController()
        
        photoBrowserVC.shops = shops
        photoBrowserVC.index = indexPath
        
        photoBrowserVC.modalPresentationStyle = .Custom
        //        photoBrowserVC.modalTransitionStyle = .FlipHorizontal
        photoBrowserVC.transitioningDelegate = animator
        
        presentViewController(photoBrowserVC, animated: true, completion: nil)
    
    }


}


